package com.example.myanimator.six;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/26
 * @describe
 */
public class GetPosTanView extends View {
    private Path mCirclePath, mDstPath,linePath,mLineDstPath;
    private Paint mPaint;
    private PathMeasure mPathMeasure,mLinePathMeasure;
    private float mCurAnimValue;
    private float mLineCurAnimValue;
    private Bitmap mArrawBmp;
    private float startPoint = 0;
    private float[] pos = new float[2];
    private float[] tan = new float[2];
    private float mCenterX = 200;
    private float mCenterY = 200;
    private float mRadius = 100;



    public GetPosTanView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mArrawBmp = BitmapFactory.decodeResource(getResources(), R.drawable.left_arrow_normal);
//        Matrix m = new Matrix();
//        m.postScale(1, -1);   //镜像垂直翻转
//        m.postScale(-1, 1);   //镜像水平翻转
//        m.postRotate(180);  //旋转-90度
//        mArrawBmp = Bitmap.createBitmap(mArrawBmp, 0, 0, mArrawBmp.getWidth(), mArrawBmp.getHeight(), m, true);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.YELLOW);
        //画圆
        mCirclePath = new Path();
        mDstPath = new Path();
        mCirclePath.addCircle(mCenterX, mCenterY, mRadius, Path.Direction.CW);
        mPathMeasure = new PathMeasure(mCirclePath, false);

        //画勾
        linePath = new Path();
        mLineDstPath = new Path();
        linePath.moveTo(mCenterX-mRadius/2,mCenterY);
        linePath.lineTo(mCenterX,mCenterY+mRadius/2);
        linePath.lineTo(mCenterX+mRadius/2,mCenterY-mRadius/2);
        mLinePathMeasure = new PathMeasure(linePath,false);


        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurAnimValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(3000);
        animator.start();

        ValueAnimator animator1 = ValueAnimator.ofFloat(0, 1);
        animator1.setRepeatCount(ValueAnimator.INFINITE);
        animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mLineCurAnimValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
//        animator1.setStartDelay(3000);
        animator1.setDuration(3000);
        animator1.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawCircle2(canvas);


    }

    private void drawCircle2(Canvas canvas) {
        canvas.translate(400, 300);
        float endLength = (float) (mPathMeasure.getLength() * mCurAnimValue);
//        startPoint =(float) ((0.9*mPathMeasure.getLength()));
        startPoint = (float) (endLength - ((0.5 - Math.abs(mCurAnimValue - 0.5)) *
                mPathMeasure.getLength()));
        mDstPath.reset();
        Log.d("tgw-PathMeasure", "mPathMeasure-startPoint: " + startPoint + "圆绘制 动画进度" + mCurAnimValue);
        mPathMeasure.getSegment(0, endLength, mDstPath, true);
        canvas.drawPath(mDstPath, mPaint);

//        mPathMeasure.getPosTan(endLength, pos, tan);
//        float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI);
//        Matrix matrix = new Matrix();
//        matrix.postRotate(degrees, mArrawBmp.getWidth() / 2, mArrawBmp.getHeight() / 2);
//        matrix.postTranslate(pos[0] - mArrawBmp.getWidth() / 2, pos[1] - mArrawBmp.getHeight() / 2);
//        canvas.drawBitmap(mArrawBmp, matrix, mPaint);


        //加箭头 该方法 与  getPosTan 的方法实现效果一致
        Matrix matrix1 = new Matrix();
        mPathMeasure.getMatrix(endLength, matrix1, PathMeasure.POSITION_MATRIX_FLAG |
                PathMeasure.TANGENT_MATRIX_FLAG);
        matrix1.preTranslate(-mArrawBmp.getWidth() / 2, -mArrawBmp.getHeight() / 2);
        canvas.drawBitmap(mArrawBmp, matrix1, mPaint);


        //加钩 路径动画
        float lineEndLength = (float) (mLinePathMeasure.getLength() * mLineCurAnimValue);
        mLineDstPath.reset();
        mLinePathMeasure.getSegment(0, lineEndLength, mLineDstPath, true);
        canvas.drawPath(mLineDstPath, mPaint);

    }
}